# 构造器注入
import json


class Person(object):
    def __init__(self, name, age, sex):
        # name字段为私有化
        self.__name = name
        self.__age = age
        self.__sex = sex

    # toString方法
    def __str__(self):
        return "{" + f"'name':{self.__name},'age':{self.__age},'sex':{self.__sex}" + "}"

    def __delattr__(self, name: str) -> None:
        super().__delattr__(name)

    def eat(self, food):
        print(self.__name + "今天早上吃:" + food)

    def sleep(self, time):
        print(self.__name + "一天睡:" + str(time) + "小时!")


class Student(object):

    def eat(self, food):
        print(self.name + "今天早上吃:" + food)

    def sleep(self, time):
        print(self.name + "一天睡:" + str(time) + "小时!")

    def learn(self, time):
        print(self.name + "一天学习:" + str(time) + "小时!")
        # toString方法

    def __str__(self):
        return "{" + f"'name':{self.__name},'age':{self.__age},'sex':{self.__sex}" + "}"


class User(object):

    def __init__(self, user_name, age, sex, hobby) -> None:
        self.userName = user_name
        self.age = age
        self.sex = sex
        self.hobby = hobby

    def __str__(self):
        return json.dumps(self, ensure_ascii=False, default=lambda obj: obj.__dict__)


class Hobby:

    def __init__(self, **kwargs) -> None:
        self.hobby = kwargs['hobby']
        self.desc = kwargs['desc']

    # toString方法
    def __str__(self):
        return "{" + f"'hobby':{self.hobby},'desc':{self.desc}" + "}"

    def get_hobby_name(self):
        return self.hobby


# 继承
class Car(object):

    def __init__(self, **kwargs) -> None:
        self.band = kwargs['band']
        self.price = kwargs['price']

    def run(self):
        pass

    # toString方法
    def __str__(self):
        return json.dumps(self, ensure_ascii=False, default=lambda obj: obj.__dict__)


class BenChi(Car):

    def __init__(self, **kwargs) -> None:
        super().__init__(band=kwargs['band'], price=kwargs['price'])
        self.classify = kwargs['classify']

    def zhuang_b(self):
        print("开出去可以装逼啊")

    def run(self):
        print(self.band + ":"+self.classify + ":舒适,快捷!!")


# 创建对象
xueXue = Person('学学', 16, '男')
print(xueXue)
# print(id(xueXue))
xueXue.eat("包子")
xueXue.sleep(8)
# 动态添加属性值
stu = Student
stu.name = '小明'
stu.age = 14
stu.sex = '男'
# 复杂对象
hobby_list = []
hobby_1 = Hobby(hobby='coding', desc='写代码啊')
hobby_2 = Hobby(hobby='旅游', desc='出去浪啊')
hobby_list.append(hobby_1)
hobby_list.append(hobby_2)

user = User('孙悟空', 500, '男', hobby_list)
print(user)
print(json.dumps(user.hobby, ensure_ascii=False, default=lambda obj: obj.__dict__))
# print(user.userName)

# 多态
ben_chi_car = Car(band='奔驰', classify='SUV', price=500000)
ben_chi_car_1 = BenChi(band='奔驰', classify='SUV', price=500000)
ben_chi_car.run()
ben_chi_car_1.run()
# print(ben_chi_car)
# print(ben_chi_car_1)
# print(ben_chi_car.zhuang_b())
